package com.guocheng.modules.emp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.dictionarie.model.Dictionarie;
import com.guocheng.modules.dictionarie.utils.Dictionaries;
import com.guocheng.modules.emp.model.po.Emp;
import com.guocheng.modules.emp.model.dto.EmpDTO;
import com.guocheng.modules.emp.service.EmpService;
import com.guocheng.modules.login.model.dto.DeptDTO;
import com.guocheng.modules.login.model.po.Dept;
import com.guocheng.modules.login.service.DeptService;
import com.guocheng.modules.login.utils.DeptUtils;
import com.guocheng.utils.FileUtils;
import com.mchange.lang.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/index")
    public String index() {
        return "emp/list";
    }

    /**
     * 查询员工信息列表
     *
     * @param empDTO
     * @param
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult viewsEmp(EmpDTO empDTO) {
        //姓名
        String name = empDTO.getName();
        if (name != null && !name.equals("")) {
            empDTO.setName("like_" + name);
        }
        //工号
        String jobNumber = empDTO.getJobNumber();
        if (jobNumber != null && !jobNumber.equals("")) {
            empDTO.setJobNumber("like_" + jobNumber);
        }
        //证件号码
        String number = empDTO.getNumber();
        if (number != null && !number.equals("")) {
            empDTO.setNumber("like_" + number);
        }

        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();

        //开始入职时间
        Date startDate = empDTO.getStartDate();
        if (startDate != null)
            queryWrapper.ge("entry_date", startDate);

        //结束入职时间
        Date endDate = empDTO.getEndDate();
        if (endDate != null)
            queryWrapper.le("entry_date", endDate);

        //分页查询
        IPage<Emp> empIPage = empService.selectPage(empDTO, queryWrapper);
        //查询照片信息
        List<Emp> records = empIPage.getRecords();

        return AjaxResult.ok().data(records).total(empIPage.getTotal());
    }

    /**
     * 根据id查询照片信息
     * @param id
     * @return
     */
    @GetMapping("/photos")
    @ResponseBody
    public AjaxResult viewPhotos(@RequestParam("id")Integer id){
        List<String> photos = empService.selectPhotosById(id);
        return AjaxResult.ok().data(photos);
    }

    /**
     * 多选人员页面
     *
     * @return
     */
    @GetMapping("/select/list")
    public String selectList() {
        return "emp/selectlist";
    }

    /**
     * 单选人员页面
     *
     * @return
     */
    @GetMapping("/select/one")
    public String selectOne() {
        return "emp/select";
    }

    /**
     * 分页无搜索查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/select")
    @ResponseBody
    public AjaxResult selectEmp(@RequestParam(value = "page", required = false) Integer page,
                                @RequestParam(value = "limit", required = false) Integer limit) {
        if (page != null && limit != null) {
            IPage<Emp> empIPage = empService.selectPage(new Emp().setStatus(1), page, limit);
            return AjaxResult.ok().data(empIPage.getRecords()).total(empIPage.getTotal());
        }
        List<Emp> emps = empService.selectList(new Emp().setStatus(1));
        return AjaxResult.ok().data(emps);
    }

    /**
     * 打开展示页面
     *
     * @return
     */
    @GetMapping("/show")
    public String createUser(@RequestParam(value = "id", defaultValue = "-1") Integer id, Model model) {
        if (id.intValue() != -1) {
            Emp emp = empService.selectById(id);
            model.addAttribute("emp", emp);
        }
        model.addAttribute("classes", Dictionaries.getDictionaries("empType"));
        return "emp/show";
    }

    /**
     * 增加一个人员
     *
     * @param emp
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult createEmp(Emp emp) {
        //判断名称是否为空
        if (StringUtils.isEmpty(emp.getName())) {
            return AjaxResult.error("姓名不能为空");
        }
        //判断工号是否为空
        if (StringUtils.isEmpty(emp.getJobNumber())) {
            return AjaxResult.error("工号不能为空");
        }
        //人员类别
        if(emp.getClassesId()!=null){
            Dictionarie empType = Dictionaries.getDictionaries("empType", emp.getClassesId());
            emp.setClassessName(empType==null?null:empType.getName());
        }
        //默认开启
        emp.setStatus(1);
        boolean insert = empService.insert(emp);
        if (insert) {
            return AjaxResult.ok("添加成功");
        }
        return AjaxResult.error("添加失败");
    }

     /**
     * 更改一个emp
     *
     * @param emp
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult updateEmp(Emp emp) {
        Integer id = emp.getId();
        if (id == null) {
            return AjaxResult.error("没有id");
        }
        if(emp.getStatus()!=null){
            empService.updateById(emp);
            return AjaxResult.ok();
        }

        //判断名称是否为空
        if (StringUtils.isEmpty(emp.getName())) {
            return AjaxResult.error("姓名不能为空");
        }
        //判断工号是否为空
        if (StringUtils.isEmpty(emp.getJobNumber())) {
            return AjaxResult.error("工号不能为空");
        }
        //人员类别
        if(emp.getClassesId()!=null){
            Dictionarie empType = Dictionaries.getDictionaries("empType", emp.getClassesId());
            emp.setClassessName(empType==null?null:empType.getName());
        }
        boolean b = empService.updateById(emp);
        if (b) {
            return AjaxResult.ok("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 删除一个后台用户
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delete(@RequestParam("ids") List<Integer> ids) {
        int flag = empService.deleteByIds(ids);
        if (flag > 0) {
            return AjaxResult.ok("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 打开图片上传页面
     *
     * @param id
     * @return
     */
    @GetMapping("/upload")
    public String upload(@RequestParam("id") Integer id,Model model) {
        List<String> photos = empService.selectPhotosById(id);
        for (int i = photos.size(); i<3; i++) {
            photos.add("/static/images/imageback.jpg");
        }
        for (int i = 1; i < 4; i++) {
            model.addAttribute("photo"+i,photos.get(i-1));
        }
        return "emp/upload";
    }

    /**
     * 接收文件地方
     *
     * @param file
     * @param id
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("id") Integer id) throws IOException {
        System.out.println(file.getSize());
        System.out.println(id);
        System.out.println(file.getOriginalFilename());
        FileUtils.write(file.getOriginalFilename(),file.getBytes());
        return AjaxResult.ok();
    }


}
